package com.tomkk;

import com.google.gson.Gson;
import com.tomkk.entity.User;
import com.tomkk.utils.Error;
import com.tomkk.utils.MysqlOperation;
import com.tomkk.utils.Success;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Tomkk on 2016/5/21.
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/login")//必须加斜线
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        Gson gson = new Gson();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        MysqlOperation operation = new MysqlOperation();
        operation.checkConn();
        ResultSet resultSet = operation.login(username,password);
        try {
            if(resultSet == null){
                Error res = new Error(1,"服务器错误");
                String json = gson.toJson(res);
                PrintWriter writer = response.getWriter();
                writer.print(json);
                writer.flush();
            }
            else if(resultSet.next()){
                int role = Integer.parseInt(resultSet.getString("role"));
                boolean isuse = resultSet.getBoolean("user_isuse");
                String json;
                if(!isuse){
                    Error res = new Error(1,"账号已被停用");
                    json = gson.toJson(res);
                }
                else if(role==0){
                    Success res = new Success(0,"账号还在等待审核");
                    json = gson.toJson(res);
                }
                else {
                    String nickname = resultSet.getString("nickname");
                    String email = resultSet.getString("email");
                    String mobile = resultSet.getString("mobile");
                    String realname = resultSet.getString("realname");
                    String headimage = resultSet.getString("headimage");
                    int id = resultSet.getInt("user_id");

                    User user = new User(id,username,nickname,email,mobile,realname,role,headimage,isuse);
                    Success res = new Success(0,user);//返回用户身份
                    json = gson.toJson(res);
                }

                PrintWriter writer = response.getWriter();
                writer.print(json);
                writer.flush();
            }
            else{
                Error res = new Error(1,"账号密码错误");
                String json = gson.toJson(res);
                PrintWriter writer = response.getWriter();
                writer.print(json);
                writer.flush();
            }
            operation.closeDB();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
